package com.ang.jdk8.stream;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ssang
 * @date 2023-01-12 17:52
 */
public class DemoTest {

    public static void main(String[] args) {
        DemoTest test = new DemoTest();
        test.test01();
    }

    /**
     * 获取list中，type属性重复个数大于1的type集合
     */
    public void test01(){
        List<Model1> list = this.getModel1List();
        //重复的编码集合
        List<String> resultList = list.stream().collect(Collectors.groupingBy(Model1::getType, Collectors.counting()))
                .entrySet().stream().filter(e -> e.getValue() > 1)
                .map(Map.Entry::getKey).collect(Collectors.toList());

        resultList.forEach(System.out::println);


        List<Map.Entry<String, Long>> list1 = list.stream().collect(Collectors.groupingBy(Model1::getType, Collectors.counting()))
                .entrySet().stream().filter(e -> e.getValue() > 1).collect(Collectors.toList());

        list1.forEach(System.out::println);
    }


    /**
     * 辅助实体类
     */
    @Data
    @AllArgsConstructor
    class Model1 {
        private Integer id;
        private String type;
        private String msg;
    }

    public List<Model1> getModel1List(){
        List<Model1> list = new ArrayList<>();
        Model1 m1 = new Model1(1,"A","a1");
        Model1 m2 = new Model1(2,"A","a2");
        Model1 m3 = new Model1(3,"B","b1");
        Model1 m4 = new Model1(4,"C","c1");
        Model1 m5 = new Model1(5,"C","c2");
        Model1 m6 = new Model1(6,"C","c3");
        list.add(m1);
        list.add(m2);
        list.add(m3);
        list.add(m4);
        list.add(m5);
        list.add(m6);

        return list;
    }
}
